package com.ysstech.db.vcs.listener;

import liquibase.change.core.TagDatabaseChange;
import liquibase.changelog.ChangeSet;
import liquibase.changelog.DatabaseChangeLog;
import liquibase.changelog.visitor.AbstractChangeExecListener;
import liquibase.database.Database;
import org.springframework.stereotype.Component;

/**
 * 每一个更改集自动添加tag
 *
 * @author pengzhikang
 * @date 2024/12/3 19:50
 * @since 1.0.0
 */
@Component
public class AutoTagChangeExecListener extends AbstractChangeExecListener {

    /** 标签前缀 */
    public static final String TAG_PREFIX = "TAG-";

    @Override
    public void willRun(ChangeSet changeSet, DatabaseChangeLog databaseChangeLog, Database database, ChangeSet.RunStatus runStatus) {
        if (changeSet != null && changeSet.getChanges().stream().noneMatch(TagDatabaseChange.class::isInstance)) {
            TagDatabaseChange tagDatabaseChange = new TagDatabaseChange();
            tagDatabaseChange.setTag(TAG_PREFIX + changeSet.getId());
            changeSet.addChange(tagDatabaseChange);
        }
    }

}
